package edu.ucsc.gameAI.conditions;

import java.util.Vector;

import pacman.game.Game;
import pacman.game.Constants.DM;
import pacman.game.Constants.GHOST;
import edu.ucsc.gameAI.ICondition;

public class IsDangerousGhostCloserThanEdibleGhost implements ICondition{


	@Override
	public boolean test(Game game) {
		int pacManCurrentNode = game.getPacmanCurrentNodeIndex();
		
		Vector<GHOST> edibleGhosts = new Vector<GHOST>();
		Vector<GHOST> nonEdibleGhosts = new Vector<GHOST>();
		for (GHOST ghost: GHOST.values()){
			if(game.isGhostEdible(ghost)){ //If any Ghosts Are Edible
				edibleGhosts.add(ghost); // store which ghosts are edible.	
			}
			else{
				nonEdibleGhosts.add(ghost);
			}
		}
		
		double closestEdibleGhostDistance = Integer.MAX_VALUE;
		double closestDangerGhostDistance = Integer.MAX_VALUE;

		for (GHOST edibleGhost: edibleGhosts) {

			int currentGhostIndex = game.getGhostCurrentNodeIndex(edibleGhost);
			double currentDistance = game.getDistance(pacManCurrentNode, currentGhostIndex, DM.PATH);
			
			if(closestEdibleGhostDistance > currentDistance)
				closestEdibleGhostDistance = currentDistance;
		}
		
		for (GHOST dangerGhost: nonEdibleGhosts) {

			int currentGhostIndex = game.getGhostCurrentNodeIndex(dangerGhost);
			double currentDistance = game.getDistance(pacManCurrentNode, currentGhostIndex, DM.PATH);
			
			if(closestDangerGhostDistance > currentDistance)
				closestDangerGhostDistance = currentDistance;
		}
			
		return closestDangerGhostDistance < closestEdibleGhostDistance;
	}

}
